import { defineConfig } from "vite";
// 自动引入组件
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";
import path from "path";
// 引入svg插件
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";

const pathResolve = (pathStr) => {
  return path.resolve(__dirname, pathStr);
};
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
    createSvgIconsPlugin({
      // 指定需要缓存的svg图标文件夹，即需要识别的svg都应该放在这个文件夹下
      iconDirs: [resolve(process.cwd(), "src/assets/svgs")],
      // 或
      // 指定symbolId格式（这里的配置与6.2步骤中的引入svg组件的name配置项写法有关）
      symbolId: "icon-[dir]-[name]",
    }),
  ],
  base: "/", //打包后，访问的路径环境变量
  resolve: {
    alias: {
      "@": pathResolve("./src"),
    },
  },
  // 配置代理
  server: {
    host: "0.0.0.0",
    cors: true, // 允许跨域
    port: 5173, // 端口号
    proxy: {
      "/api": {
        secure: true, // 允许https请求
        // target: "http://192.168.8.175:32319", //跨域地址
        target: "http://121.43.182.130:32319", //梵迩后端地址
        // target: "https://zhbscs.ittime.com", //望宸智慧后端地址
        changeOrigin: true, //支持跨域
        rewrite: (path) => path.replace(/^\/api/, ""), //重写路径,替换/api
      },
    },
  },
});
